#include "gUSBImp.h"

gUSBImp::gUSBImp(wxWindow* parent):ImpCheck2(parent)
{
	esci = false;
}
gUSBImp::~gUSBImp()
{

}

#ifdef HAVE_GTEC_USB_LIBRARY
void* gUSBImp::Entry(void)
{
	double Impedance;

	while (!esci)
	{
		if (this->IsShown())
		{
			if (hGT_GetImpedance(hDevice, 1, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh1,NameCh1);
			else
				break;
			if (hGT_GetImpedance(hDevice, 2, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh2,NameCh2);
			else
				break;
			if (hGT_GetImpedance(hDevice, 3, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh3,NameCh3);
			else
				break;
			if (hGT_GetImpedance(hDevice, 4, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh4,NameCh4);
			else
				break;
			if (hGT_GetImpedance(hDevice, 5, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh5,NameCh5);
			else
				break;
			if (hGT_GetImpedance(hDevice, 6, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh6,NameCh6);
			else
				break;
			if (hGT_GetImpedance(hDevice, 7, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh7,NameCh7);
			else
				break;
			if (hGT_GetImpedance(hDevice, 8, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh8,NameCh8);
			else
				break;
			if (hGT_GetImpedance(hDevice, 9, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh9,NameCh9);
			else
				break;
			if (hGT_GetImpedance(hDevice, 10, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh10,NameCh10);
			else
				break;
			if (hGT_GetImpedance(hDevice, 11, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh11,NameCh11);
			else
				break;
			if (hGT_GetImpedance(hDevice, 12, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh12,NameCh12);
			else
				break;
			if (hGT_GetImpedance(hDevice, 13, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh13,NameCh13);
			else
				break;
			if (hGT_GetImpedance(hDevice, 14, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh14,NameCh14);
			else
				break;
			if (hGT_GetImpedance(hDevice, 15, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh15,NameCh15);
			else
				break;
			if (hGT_GetImpedance(hDevice, 16, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpCh16,NameCh16);
			else
				break;
			if (hGT_GetImpedance(hDevice, 17, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpRef1,NameRef1);
			else
				break;
			if (hGT_GetImpedance(hDevice, 18, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpRef2,NameRef2);
			else
				break;
			if (hGT_GetImpedance(hDevice, 19, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpRef3,NameRef3);
			else
				break;
			if (hGT_GetImpedance(hDevice, 20, &Impedance) && (!esci))
				CheckImpedance(Impedance,ImpRef4,NameRef4);
			else 
				break;
				
		}
		else
		{
			Sleep(100);
		}
	}
	
	hGT_CloseDevice(&hDevice);
	hGT_ResetTransfer(hDevice);
	hDevice=NULL;
	return NULL;
}
int gUSBImp::Connect(const char *OpenVal1)
{
	hDevice = hGT_OpenDevice(atoi(OpenVal1));
	if (hDevice == NULL)
		return -1;

	UCHAR _mode = M_IMPEDANCE;		
	if (!hGT_SetMode(hDevice, _mode))
		return -1;

	connect = true;
	return 0;
}

void gUSBImp::OnStart(void)
{
	if (hDevice != NULL)
	{
		wxThread::Create();
		Run(); //Run thread
	}
}
void gUSBImp::OnStop(void)
{
	if (esci == false)
	{
		esci = true;
		Sleep(500);
	}
}

void gUSBImp::CheckImpedance(double imp, wxStaticText *Value, wxStaticText *ValueName)
{
	if (imp==NULL)
		return;
	if (imp<0)
	{
		Value->SetBackgroundColour(wxColor(0,0,0));
		Value->SetForegroundColour(wxColor(255,255,255));
		Value->SetLabel(_T("NC"));
	}
	else if (imp<5000)
	{
		Value->SetLabel(wxString::Format(_("%.0f ohm"),imp));
		Value->SetBackgroundColour(wxColour(0,255,0)); //Good
		Value->SetForegroundColour(wxColor(0,0,0));
	}
	else if (imp >50000) 
	{
		Value->SetBackgroundColour(wxColor(0,0,0));
		Value->SetForegroundColour(wxColor(255,255,255));
		Value->SetLabel(_T("NC"));
	}
	else
	{
		Value->SetLabel(wxString::Format(_("%d K"),imp/1000));	
		if ((imp)<5000)
			Value->SetBackgroundColour(wxColour(0,255,0)); //Good
		else if ((imp)<10000)
			Value->SetBackgroundColour(wxColour(255,255,0)); //Medium
		else 
			Value->SetBackgroundColour(wxColour(255,0,0)); //Bad
		Value->SetForegroundColour(wxColor(0,0,0));
	}
	Value->SetSize(70,20);
	NameCh1->SetForegroundColour(wxColor(0,0,255));
	
	this->Layout();

}
#endif